package com.cjm.algorithm.backtracking;

import java.util.*;

public class Ti17 {
    public static void main(String[] args) {
        Ti17 ti17 = new Ti17();
        ti17.letterCombinations("23");
        System.err.println("OK");
    }
    private static Map<Integer, List<Character>> xx = new HashMap<>();
    static {
        xx.put(2, Arrays.asList('a', 'b', 'c'));
        xx.put(3, Arrays.asList('d', 'e', 'f'));
        xx.put(4, Arrays.asList('g', 'h', 'i'));
        xx.put(5, Arrays.asList('j', 'k', 'l'));
        xx.put(6, Arrays.asList('m', 'n', 'o'));
        xx.put(7, Arrays.asList('p', 'q', 'r', 's'));
        xx.put(8, Arrays.asList('t', 'u', 'v'));
        xx.put(9, Arrays.asList('w', 'x', 'y', 'z'));
    }
    private List<String> res = new ArrayList<>();
    public List<String> letterCombinations(String digits) {
        res.clear();
        if (digits == null || digits.isEmpty()) {
            return new ArrayList<>();
        }
        method17(0, digits, "");
        return res;
    }

    private void method17(int startIndex, String digits, String stringBuilder) {
        if (stringBuilder.length() == digits.length()) {
            res.add(stringBuilder);
            return;
        }
        char c = digits.charAt(startIndex);
        Integer i1 = Integer.valueOf(String.valueOf(c));
        List<Character> characters = xx.get(i1);
        for (int i = 0; i < characters.size(); i++) {
            stringBuilder+=(characters.get(i));
            method17(startIndex + 1, digits, stringBuilder);
            stringBuilder = stringBuilder.substring(0, stringBuilder.length() - 1);
        }
    }
}
